#include "StdAfx.h"
#include ".\ipmonitor.h"
#include "DnsQueryWorker.h"
#include "DnsCore.h"
#include "DnsSocket.h"

DWORD g_dwDnsRecordIp = INADDR_NONE;
DWORD g_dwDnsRecordUpdateTick = 0;




int  GetDnsRecordIpProc(void)
{
	char szResult[64] = {0};
	CDnsQueryWorker query;

	unsigned short id = (unsigned short) GetTickCount() % 0xFFFF;

	query.QueryDns( g_strDnsDomain.c_str(), g_strDynQueryDns.c_str(), id , DNS_TYPE_A );

	int nStatus = 0;

	int i = 0;
	for ( i = 0 ; i < 20 ; i++ )
	{
		nStatus = query.Update();
		if( nStatus != DNS_WORKER_STATUS_QUERY )
			break;

		Sleep( 20 );
	}
	
	if( nStatus == DNS_WORKER_STATUS_EXISTS )
	{
		g_dwDnsRecordUpdateTick = GetTickCount( );
		strcpy( szResult, query.GetAddress() );
		g_dwDnsRecordIp = inet_addr( szResult );		
	}

	return 0;
}


ULONG GetInternetIPAddress()
{
	char szResult[64] = {0};
	CDnsQueryWorker query;

	unsigned short id = (unsigned short) GetTickCount() % 0xFFFF;

	query.QueryDns( "myip.opendns.com", "208.67.222.222", id , DNS_TYPE_A, 5353 );

	int nStatus = 0;	
	
	for (int i = 0; i < 10; i++)
	{
		nStatus = query.Update( 500 );
		if( nStatus != DNS_WORKER_STATUS_QUERY )
			break;
		Sleep(20);
	}	

	if( nStatus == DNS_WORKER_STATUS_EXISTS )
	{		
#ifdef _DEBUG
		IPAddressEx ip;
		ip.dwIP =  inet_addr( query.GetAddress() );
#endif
		return	 inet_addr( query.GetAddress() );
	}

	return INADDR_NONE;
}

int GetDnsRecordIpProc( PIP4_ARRAY lpDnsArray )
{
	ULONG ip = INADDR_NONE;
	
	ip = GetPublicHostRecord( lpDnsArray, g_strDnsDomain.c_str() );
	if( ip != INADDR_NONE )
	{
		g_dwDnsRecordIp = ip ;
		return TRUE;
	}

	return FALSE;
}

ULONG  GetLocalIpProcEx(void)
{
	ULONG  remoteIP = 0;
	struct sockaddr_in SocketAddress;
	struct hostent     *pHost   = 0;
	pHost = ::gethostbyname( "fw.qq.com" );
	if(pHost)
	{
		//char aszIPAddresses[10][16]; // maximum of ten IP addresses
		//for(int iCnt = 0; ((pHost->h_addr_list[iCnt]) && (iCnt < 10)); ++iCnt)
		//{			
		memcpy(&SocketAddress.sin_addr, pHost->h_addr_list[0], pHost->h_length);
		//}

		remoteIP = SocketAddress.sin_addr.S_un.S_addr;
	}
	else
	{
		return INADDR_NONE;
	}

	CDnsSocket ds;
	if( ds.Conn( remoteIP, 80 ) != TRUE )
	{
		return INADDR_NONE;
	}

	int nResult = 0;
	int i = 0;

	string strRequest = GetTencentIpRequest( );
	nResult = ds.Write( strRequest.c_str(), strRequest.size() );

	char szBuffer[BUFFER_SIZE] = {0};
	int nCount = 0;
	for(i = 0; i < 200; i++ )
	{
		if( ds.IsReadable( 100 ) == TRUE )
		{			
			nResult = ds.Read( szBuffer + nCount , BUFFER_SIZE-nCount );
			if( nResult <= 0 )
			{
				ds.Disconnect();
				break;
			}
			else
			{
				nCount += nResult;
			}
		}
		
	}

//	ds.Disconnect();

	char* lpDataStart = strstr( szBuffer, "IPData" ) ;

	if( lpDataStart == NULL )
		return INADDR_NONE;

	char szIP[256] = {0};
	char* lpIpStart = strstr( lpDataStart, "\"");
	if( lpIpStart == NULL )
		return INADDR_NONE;

	char* lpIpEnd = strstr( lpIpStart + 2, "\"");
	if( lpIpEnd == NULL )
		return INADDR_NONE;

	int nBytes = (int)( lpIpEnd - lpIpStart );
	strncpy( szIP, lpIpStart +1, nBytes -1 );

    ULONG  dwLocalIp = inet_addr( szIP );

	return dwLocalIp;
}

string GetTencentIpRequest( void )
{
	string strContent;

	strContent = "GET /ipaddress HTTP/1.1\r\n";
	strContent += "Accept: */*\r\n";
//	strContent += "Accept-Encoding: gzip, deflate\r\n";	
	strContent += "User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET4.0C; .NET4.0E)\r\n";
	strContent += "Host: fw.qq.com\r\n";	
    strContent += "Connection: Keep-Alive\r\n";
	strContent += "\r\n";

	return  strContent;

}
